
import {annualPrice } from '@/api/api'
import {getDataBaseExpense,getServerExpense,getServerParams,getHistoryList} from '@/api/api'
// export const ipTransform = function(){
//       var val = this.$refs.ips.$refs.input.value;
//       if(val.indexOf("，")>-1){
//         this.$refs.ips.$refs.input.value = val.replace("，",",");
//       }
//     }
export const getAnnualPrice = function(num,date){
  return new Promise((resolve ,reject)=>{
    annualPrice({}).then((res) => {
      if(res != ""){
        var day = myDate();
        var residue = residueDay(date);
        resolve((res/day*residue).toFixed(2)*num)
      }
    })
  })
}
export const residueDay = function(openTime){//获取当前年份剩余天数
  var date = new Date();
  if(openTime!=undefined){
    date = new Date(openTime);
  }
  var year = date.getFullYear();
  var date2 = new Date(year, 11, 30, 23, 59, 59,999);//
  /*转换成秒*/
  var time = (date2 - date) / 1000;
  var day = Math.floor(time / (24 * 60 * 60))
  var hour = Math.floor(time % (24 * 60 * 60) / (60 * 60))
  var minute = Math.floor(time % (24 * 60 * 60) % (60 * 60) / 60);
  var second = Math.floor(time % (24 * 60 * 60) % (60 * 60) % 60);
  if(hour > 0 || minute > 0 || second > 0){
    day++;
  }
  return day;
}
export const myDate = function(t) {//t 代表指定的参数 t为年份
  if (t == null) {
    var Year = new Date().getFullYear(), s = 0, d;//获取当前年
    for (var i = 1; i < 13; i++) {
      d = new Date(Year, i, 0);//获取某一个月的天数
      s += d.getDate();
    }
    ;
    return s;
  } else if (t >= 1970) {
    var Year = new Date().getFullYear(), s = 0, d;
    for (var i = 1; i < 13; i++) {
      d = new Date(t, i, 0);
      s += d.getDate();
    }
    ;
    return s;
  } else {
    return "年份有误";
  }
}
  export const getServerExpense1 = function(name,date) {
    var day = myDate();
    var residue = residueDay(date);
    let year = new Date().getFullYear();
    let serverName = {serverName: name};
    let yearDate = {yearDate: year};
    let formData = Object.assign(serverName, yearDate);
    return new Promise((resolve) => {
      getServerExpense(formData).then((res) => {
        if (res.success) {
          // resolve((res/day*residue).toFixed(2)*num)
          resolve((res.result.money / day * residue));
          // var money=res.result.money;
        } else {
          resolve(0);
          alert("请先维护云服务器套餐");
        }
      })

    });
  }
    export const getDataBaseName = function(name,date) {
      var day = myDate();
      var residue = residueDay(date);
      let year = new Date().getFullYear();
      year=year.toString();
      let dataBaseName = {dataBaseName: name};
      let yearDate = {yearDate: year};
      let formData = Object.assign(dataBaseName, yearDate);
      return new Promise((resolve) => {
        getDataBaseExpense(formData).then((res) => {
          if (res.success) {
            // resolve((res/day*residue).toFixed(2)*num)
            resolve((res.result.expense / day * residue));
            // var money=res.result.money;
          } else {
            resolve(0);
            alert("请先维护数据库套餐");
          }
        })

      });
    }
export const getServerParam = function(cpu,disk,hardpan,date) {
  var day = myDate();
  var residue = residueDay(date);
  let year = new Date().getFullYear();
  year = year.toString();
  let cpuVal = {cpuVal: '5'};
  let diskVal = {diskVal: '6'};
  let hardpanVal = {hardpanVal: '7'};
  let yearDate = {yearDate: year};
  let formData = Object.assign(cpuVal, diskVal, hardpanVal, yearDate);
  return new Promise((resolve) => {
    getServerParams(formData).then((res) => {
      let sum = 0;
      debugger;
      if (res.success) {
        let cpuV = res.result.cpuValue;
        let diskV = res.result.diskValue;
        let hardpanV = res.result.hardpanValue;
        if (cpuV != null && cpu != null && !isNaN(cpu)) {
          sum = parseFloat((sum + (cpuV / day * residue)).toFixed(2));
        }
        if (diskV != null && !isNaN(disk)) {
          sum = parseFloat((sum + (diskV / day * residue * disk)).toFixed(2));
        }
        if (hardpanV != null && !isNaN(hardpan)) {
          sum = parseFloat((sum + (hardpanV / day * residue * hardpan)).toFixed(2));
        }
        // resolve((res/day*residue).toFixed(2)*num)
        resolve(sum);
        // var money=res.result.money;
      } else {
        resolve(sum);
        alert("请先维护数据库套餐");
      }
    })

  });
}
  export const getHistoryListId = function(id) {
    let formData = Object.assign(id);
    return new Promise((resolve) => {
      getHistoryList(formData).then((res) => {
        if (res.success) {
          debugger
          let result=res.list;
          resolve(result);
        } else {
          resolve(0);
        }
      })
    })
  }


